package mysql

import (
	"hippo-fresh/hippofreshrpc/hippofreshrpc"

	"gorm.io/gorm"
)

// 商品

type Shop struct {
	gorm.Model
	Style   string  `gorm:"type:varchar(30);not null;comment:商品类型"`
	Name    string  `gorm:"type:varchar(30);not null;comment:商品名称"`
	Price   float64 `gorm:"type:float;not null;comment:商品价格"`
	ClassId int64   `gorm:"type:int;not null;comment:分类ID"`
}

// 商品添加

func (s *Shop) ShopAdd(db *gorm.DB) error {
	return db.Debug().Create(s).Error
}

func (s *Shop) FindGoodByName(db *gorm.DB, name string) error {
	return db.Debug().Where("name=?", name).First(s).Error
}

// 商品查询

func (s *Shop) ShopSelect(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).Find(s).Error
}

// 商品列表

func ShopList(db *gorm.DB, in *hippofreshrpc.ShopListRequest) []*hippofreshrpc.ShopList {
	/*
		select
		shops.id as ShopId,
		shops.style as ShopStyle,
		shops.`name` as ShopName,
		shops.price as ShopPrice,
		classes.`name` as ClassName
		from shops
		left join classes on shops.class_id = classes.id
	*/

	var list []*hippofreshrpc.ShopList
	db.Debug().
		Table("shops").
		Select(
			"shops.id as ShopId",
			"shops.style as ShopStyle",
			"shops.`name` as ShopName",
			"shops.price as ShopPrice",
			"classes.`name` as ClassName",
		).
		Joins("left join classes on shops.class_id = classes.id").
		Scopes(Paginate(int(in.Page), int(in.Size))).Find(&list)
	return list
}
